﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IISDeploy.Domain.Repository;
using IISDeploy.Domain.Core;
using NHibernate;
using IISDeploy.Data.Helper;
using NHibernate.Criterion;

namespace IISDeploy.Data.Repository
{
    public class ServerRepositoryImpl : IServerRepository
    {
        #region IServerRepository Members

        public Server GetServerByName(string name)
        {
            ISession session = NHibernateHelper.OpenSession();
            try
            {
                ICriteria criteria = session.CreateCriteria(typeof(Server));
                criteria.Add(Expression.Eq("Name", name));
                return criteria.UniqueResult<Server>();
            }
            finally
            {
                session.Close();
            }
        }

        #endregion

        #region IBaseRepository Members

        public T GetById<T>(int id)
        {
            return NHibernateHelper.FindByPrimaryKey<T>(id);
        }

        public IList<T> GetAll<T>()
        {
            return NHibernateHelper.FindAll<T>();
        }

        public void Save<T>(T item)
        {
            NHibernateHelper.Save(item);
        }

        public void Delete<T>(T item)
        {
            NHibernateHelper.Delete(item);
        }

        #endregion
    }
}
